﻿<%@ WebHandler Language="C#" Class="ErpPurchaseMfgorderReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 采购订单查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.5.23
 **/

public class ErpPurchaseMfgorderReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT erp.purchaseno,erp.qty,erp.productname,p.description productdesc,p.productrevision,erp.vendorname,erp.vendorno,ws.workflowstepname,mf.mes_factoryname,erp.status
FROM erppurchasemfgorder erp
INNER JOIN productbase pb ON erp.productname = pb.productname
LEFT JOIN product p ON p.productid = pb.revofrcdid
LEFT JOIN Workflowbase wb ON wb.workflowbaseid = p.workflowbaseid
LEFT JOIN Workflow w ON w.workflowid = NVL(wb.revofrcdid,p.workflowid)
LEFT JOIN Workflowbase wb1 ON wb1.workflowbaseid = w.workflowbaseid
LEFT JOIN workflowstep ws ON ws.workflowid = w.workflowid
LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
LEFT JOIN spec s ON s.specid = NVL(sb.revofrcdid,ws.specid)
LEFT JOIN mes_factory_mapping mf ON mf.factoryid = s.factoryid
WHERE mf.factorycode = '602'";
        
        if (jObject["purchaseno"] != null && jObject["purchaseno"].ToString() != "")
        {
            sql += string.Format(" AND erp.purchaseno = '{0}'", jObject["purchaseno"].ToString());
        }

        //订单号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND erp.productname = '{0}'", jObject["productname"].ToString());
        }
        
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getSearchSqlString(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["purchaseno"].Caption = "采购订单号";
        dtResult.Columns["qty"].Caption = "采购数量";
        dtResult.Columns["productname"].Caption = "零件号";
        dtResult.Columns["productdesc"].Caption = "零件名称";
        dtResult.Columns["productrevision"].Caption = "零件版本";
        dtResult.Columns["vendorname"].Caption = "供应商名称";
        dtResult.Columns["vendorno"].Caption = "供应商编号";
        dtResult.Columns["workflowstepname"].Caption = "工序";
        dtResult.Columns["mes_factoryname"].Caption = "加工分厂";
        dtResult.Columns.Remove("status");

        ExportHelper.ExportExcel(dtResult, "采购订单查询报表");
    }
}